﻿using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;

namespace UtilEditors
{
    public class Inspector :GUILayoutCom
    {
        public List<Component> GetComponent = new List<Component>();
        public bool IsShow = false;
        public virtual void Updata() { }
        public virtual void OnInspectorUpdate() { }
        public Rect rect;
        public string Name;
        public SubsidiaryWindow MainWin;
        public int ID;

        public virtual void Init(SubsidiaryWindow win,int windownID)
        {
            rect = new Rect(110,220,120,200);
            MainWin = win;
            ID = windownID;
            rect.x = rect.x * ID;
            Name = GetInspectorType().Name;
        }
        //选择改变
        public virtual void SelectionChange()
        {
            GetComponent.Clear();
            var targets = Selection.gameObjects;
            if(targets.Length > 0)
            {
                for(int i = 0; i < targets.Length; i++)
                {
                    var ctf = targets[i].transform;
                    var conms = ctf.GetComponents(GetInspectorType());
                    for(int j = 0; j < conms.Length; j++)
                    {
                        GetComponent.Add(conms[j]);
                    }
                }
            }
            var cs = GetComponent.Count > 0;
            if(IsShow != cs)
            {
                IsShow = cs;
                if(IsShow)
                {
                    if(ID != 0)
                    {
                        RankingPos();
                    }
                }
            }
        }
        public void RankingPos()
        {
            Rect upRect = rect;
            for(int i = ID - 1; i >= 0; i--)
            {
                if(MainWin.Alllist[i].IsShow)
                {
                    upRect = MainWin.Alllist[i].rect;
                    break;
                }
            }
            rect.x = upRect.x + upRect.width + 10;
            rect.y = upRect.y;

            for(int i = ID + 1; i < MainWin.Alllist.Count; i++)
            {
                MainWin.Alllist[i].RankingPos();
            }
        }
        //获取巡查类型,主要适用于场景对象
        public virtual Type GetInspectorType() { return default(Type); }


        public void Draw()
        {
            if(!IsShow)
            {
                return;
            }

            //绘画窗口  
            //rect = GUILayout.Window(ID, rect, DrawNodeWindow, Name);
            ComY(OnIGUI);
        }
        Vector2 scrollPos;
        // public void DrawNodeWindow(int id)
        // {
        //     scrollPos = EditorGUILayout.BeginScrollView(scrollPos);
        //     OnIGUI();
        //     EditorGUILayout.EndScrollView();
        //
        //     //rect.height = scrollPos.y.toClamp(100,1000);
        //
        //     //设置改窗口可以拖动  
        //     GUI.DragWindow();
        // }
        public virtual void OnIGUI() { }

    }

}
